
import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.nio.ByteBuffer;

/**
 * Created by zhoub on 2020/5/21.
 */
public class UdpClient {

    private int       remotePort;
    private InetAddress remoteIP;

    private DatagramSocket socket; //UDP套接字

    public UdpClient(String ip,String port)throws IOException{
        this.remotePort=Integer.parseInt(port);
        this.remoteIP=InetAddress.getByName(ip);

        //创建一个UDP套接字，与本地任意一个未使用的UDP端口绑定
        socket=new DatagramSocket();
        //与本地一个固定的UDP端口绑定
        //   socket=new DatagramSocket(9000);
    }

    //定义一个数据的发送方法。
    public void send(String msg){
        try {
            //先准备一个待发送的数据报
            byte[] outputData=msg.getBytes("GB2312");
            //构建一个数据报文。
            DatagramPacket outputPacket=new DatagramPacket(outputData,
                    outputData.length,remoteIP,remotePort);
            //给EchoUDPServer发送数据报
            socket.send(outputPacket);  //给EchoUDPServer发送数据报
        } catch (IOException ex) { }
    }

    //定义一个数据的接收方法。
    public String receive(){//throws IOException{
        String msg;
        //先准备一个空数据报文
        DatagramPacket inputPacket=new DatagramPacket(new byte[10000],10000);
        try {
            //阻塞语句，有数据就装包，以装完或装满为此.
            socket.receive(inputPacket);
            //从报文中取出字节数据并装饰成字符。
            msg=new String(inputPacket.getData(),
                    0,inputPacket.getLength(),"GB2312");
            System.out.println("receive:"+inputPacket.getLength());
        } catch (IOException ex) {
            msg=null;
        }
        return msg;
    }

    public void close(){
        if(socket!=null)
            socket.close();//释放本地端口.
    }

    public static void main(String[] args) throws IOException, InterruptedException {

        UdpClient client = new UdpClient("111.205.114.16","6680");
//        UdpClient client = new UdpClient("10.40.26.137","6680");

        /*new Thread(new Runnable() {
            @Override
            public void run() {
                String msg = null;
                while(true) {
                    try{
                        msg = client.receive();
                    } catch(Exception ex){
                        System.out.println("套接字异常关闭");
                    }
                    if(msg!=null){
                        System.out.println(msg);
                    }else{
                        System.out.println("你的对话已关闭");
                    }
                }
            }
        }).start();*/

        client.send("123456789");
       /* File file = new File("D:/audio.bin");
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
        byte[] bs = new byte[1024]; //8MB
        ByteBuffer byteBuffer = ByteBuffer.allocate(bis.available());
        int len = 0;
        InetAddress inet = InetAddress.getByName("localhost");
        while ((len = bis.read(bs)) != -1) {
            byteBuffer.put(bs,0,len);

        }
        DatagramPacket dp = new DatagramPacket(byteBuffer.array(),1200 , inet,6680);
        //创建DatagramSocket对象，数据包的发送和接收对象
        DatagramSocket ds = new DatagramSocket();
        //调用ds对象的方法send，发送数据包
        ds.send(dp);
        ds.close();*/
    }
}
